package by.msokoloff.javaweb2015.webapp.page.triporder;

import java.util.ArrayList;
import java.util.Arrays;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;

import by.msokoloff.javaweb2015.datamodel.TripOrder;
import by.msokoloff.javaweb2015.datamodel.enums.TripOrderStatus;
import by.msokoloff.javaweb2015.services.TripOrderService;
import by.msokoloff.javaweb2015.webapp.page.EntityListPage;

public class TripOrderForm extends Form<TripOrder> {

	@Inject
	private TripOrderService tripOrderService;

	private TripOrder tripOrder;

	public TripOrderForm(String id, IModel<TripOrder> model) {
		super(id, model);
		add(new Label("id"));
		add(new DateTimeField("orderDate"));
		add(new DateTimeField("payDate"));
		add(new TextField<String>("payDetails"));
		add(new TextField<String>("customersEmail"));
		add(new TextField<Integer>("qtyAdults"));
		add(new TextField<Integer>("qtyChilds"));
		add(new TextField<Integer>("qtyInfants"));
		add(new TextField<Integer>("qtyBaggage"));
		add(new CheckBox("twoWay"));
		DropDownChoice<TripOrderStatus> statusChoice = new DropDownChoice<TripOrderStatus>("status", new ArrayList<TripOrderStatus>(Arrays.asList(TripOrderStatus.values())));
		statusChoice.add(new PropertyValidator<TripOrderStatus>());
		add(statusChoice);
		
		add(new FeedbackPanel("feedbackpanel"));
		tripOrder = model.getObject();
	}

	@Override
	public final void onSubmit() {
		tripOrderService.createOrUpdate(tripOrder);
		setResponsePage(new EntityListPage(TripOrder.class));
	}

	@Override
	public final void onError() {
		System.out.println("Error!");
	}
}
